package translinkApp.views.activity;

import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.os.Bundle;
import translinkApp.views.dialog.LachDialog;
import translinkApp.views.dialog.LachDialogArguments;
import translinkApp.views.dialog.LachDialogData;

public class DialogLoaderActivityHelper {

	private DialogGeneratorList list;
	private LachDialog selectedDialog = null;
	private int selectedDialogNum = -1;

	public DialogLoaderActivityHelper(DialogLoaderActivityInf activity) {

		list = new DialogGeneratorList();
		activity.dialogGenerator(list);
	}

	public void showLachDialog(int number, LachDialogArguments args) {

		showLachDialog(number, args, null);
	}

	public void showLachDialog(int number, LachDialogArguments args, LachDialogData data) {

		DialogGenerator dialogGenerator = list.generators.get(number);
		selectedDialogNum = number;

		selectedDialog = dialogGenerator.createDialog(args, data);

		selectedDialog.setSecondaryDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface arg0) {

				selectedDialog = null;
				selectedDialogNum = -1;
			}
		});

		selectedDialog.show();
	}

	public void recreateDialog(Bundle savedInstanceState) {
		int dialogNum = savedInstanceState.getInt("dialogNum");
		LachDialogData dialogData = savedInstanceState.getParcelable("dialogSave");
		LachDialogArguments dialogArgs = savedInstanceState.getParcelable("dialogArgs");
		if (dialogData != null) {
			showLachDialog(dialogNum, dialogArgs, dialogData);
		}
	}

	public void onSaveInstanceState(Bundle outState) {

		if (selectedDialog != null) {
			outState.putInt("dialogNum", selectedDialogNum);
			outState.putParcelable("dialogSave", (LachDialogData) selectedDialog.getDialogData());
			outState.putParcelable("dialogArgs", selectedDialog.getArgs());
		}
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		try {
			selectedDialog.onActivityResult(requestCode, resultCode, data);
		} catch (Exception e) {

		}
	}

}
